﻿using Sustainalytics.Entities.SustainableProducts;
using Sustainalytics.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sustainalytics.SustainableProducts.BL.Models
{
    public class SPCompanyAssessmentDto
    {
        public virtual Guid Id { get; set; }
        public virtual string CompanyName { get; set; }
        public virtual List<SPCompanyIndicatorDto> Indicators { get; set; }
        public virtual string Country { get; set; }
        public virtual string Industry { get; set; }
        public virtual string PeerGroupName { get; set; }
        public int CompanyId { get; set; }
        public string Exchange { get; set; }
        public string Ticker { get; set; }
        public double? MarketCap { get; set; }
        public double? Employees { get; set; }
        public string CompanyNameUrlSafe { get; set; }
        public static SPCompanyAssessmentDto FromCompanyAssessment(
            SustainableProductsData ca, 
            IEnumerable<SustainableProductsFramework> indicators, 
            List<string> claimedIndicators)
        {
            var result = new SPCompanyAssessmentDto()
            {
                Id = ca.Id,
                CompanyName = ca.CompanyName,
                Country = ca.Country,
                Industry = ca.Industry,
                CompanyId = ca.CompanyId,
                Exchange = ca.Exchange,
                Ticker = ca.Ticker,
                PeerGroupName = ca.PeerGroupName,
                MarketCap = ca.MarketCap,
                Employees = ca.Employees,
                CompanyNameUrlSafe = ca.CompanyNameSafeUrl
            };

            result.Indicators = new List<SPCompanyIndicatorDto>();

            var distinctSortedIndicators = ca.Indicators.GroupBy(x => x.IndicatorSymbol).Select(grp => grp.First()).OrderBy(x => x.IndicatorSymbol);

            foreach (SustainableProductsIndicator iass in distinctSortedIndicators)
            {
                if (!claimedIndicators.Contains(iass.IndicatorSymbol))
                    continue;

                SPCompanyIndicatorDto companyIndicatorDto = new SPCompanyIndicatorDto() { Symbol = iass.IndicatorSymbol };

                var indicator = indicators.FirstOrDefault(x => x.Symbol == iass.IndicatorSymbol);
                companyIndicatorDto.Name = indicator != null ? indicator.Name : "";
                companyIndicatorDto.IndicatorAssessments = new List<SPIndicatorAssessmentDto>();

                foreach (SustainableProductsIndicator pias in ca.Indicators.Where(x => x.IndicatorSymbol == iass.IndicatorSymbol))
                {
                    //only for display purposes
                    if (!pias.LevelOfInvolvement.Stop.NearlyEq(100))
                    {
                        pias.LevelOfInvolvement.Stop -= 0.1;
                        pias.LevelOfInvolvement.Text = string.Format("{0}-{1}%", pias.LevelOfInvolvement.Start, pias.LevelOfInvolvement.Stop);
                    }

                    SPIndicatorAssessmentDto assDto = new SPIndicatorAssessmentDto()
                    {
                        IndicatorSymbol = pias.IndicatorSymbol,
                        CategorySymbol = pias.CategorySymbol,
                        HasLevelOfInvolvement = pias.HasLevelOfInvolvement,
                        LevelOfInvolvement = pias.LevelOfInvolvement,
                        CategoryOfInvolvementType = pias.CategoryOfInvolvementType,
                        ClientFacingProducts = pias.ClientFacingProducts,
                        CategoryOfInvolvementDescription = pias.CategoryOfInvolvementDescription,
                        RangeOwnership = pias.RangeOwnership,
                        RevenuePercentage = pias.RevenuePercentage,
                        ProductRevenue = pias.ProductRevenue,
                        ProductInvolvementAnalysis = pias.ProductInvolvementAnalysis,
                        RevenueAnalysis = pias.RevenueAnalysis,
                        RevenueAnalysisSource = pias.RevenueAnalysisSource,
                        Sources = pias.Sources,
                        LastEdited = pias.LastEdited,
                        FinancialYear = pias.FinancialYear
                    };

                    var cat = indicator.Categories.FirstOrDefault(x => x.Symbol == pias.CategorySymbol);
                    assDto.CategorySymbolDescription = cat.Description;
                    companyIndicatorDto.IndicatorAssessments.Add(assDto);
                }

                //order indicator assessments
                companyIndicatorDto.IndicatorAssessments = companyIndicatorDto.IndicatorAssessments.OrderBy(x => x.CategorySymbol).ToList();
                result.Indicators.Add(companyIndicatorDto);
            }

            //order indicators
            result.Indicators = result.Indicators.OrderBy(x => x.Name).ToList();
            return result;
        }

    }
}
